package com.liy.redisson;

import com.liy.redisson.entity.Product;
import com.liy.redisson.mapper.ProductMapper;
import com.liy.redisson.service.SendMailService;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author ly
 * @date 2022/11/8 10:27
 */

@SpringBootTest
public class MyCeShi {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SendMailService sendMailService;

    @Test
    public void add(){
        Product product = new Product();
        product.setName("李四");
        product.setPrice(200);
        System.out.println(product);
        int insert = productMapper.insert(product);
        System.out.println(insert);
        List<Product> products = productMapper.selectList(null);
        System.out.println(products);

    }



    @Test
    public void select(){

        //获取前一个月
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now().minus(1, ChronoUnit.MONTHS);

        //测试邮件发送
        sendMailService.sendMail();

    }

    @Test
    public void pojoList(){
        List<Product> list = new ArrayList<>();
        Product product = new Product();
        Product product2 = new Product();
        product.setId(1L);
        product2.setId(2L);
        product.setName("李四");
        product2.setName("张三");
        product.setPrice(200);
        product2.setPrice(200);
        list.add(product);
        list.add(product2);

        //pojo 转 map
        Map<Long, String> collect = list.stream().collect(Collectors.toMap(Product::getId, Product::getName));
        System.out.println(collect);

        //lambda
        Runnable to = () -> System.out.println("我是使用lambda表达式： 简洁，灵活");
        to.run();

        //参数
        Consumer<String> consumer = s -> System.out.println(s);
        consumer.accept("接受爱不一定爱对方，爱一定付出真心爱");

        //排序
        LinkedList<Integer> list1 = new LinkedList<Integer>();

        list1.add(2);
        list1.add(9);
        list1.add(7);
        list1.add(5);
        list1.add(3);
        list1.add(1);
        list1.add(0);

        Collections.sort(list1, new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        if(o1>o2) return 1;
                        else if(o1<o2) return -1;
                        else return 0;
                    }
                }
        );
        System.out.println(list1);

        //升序
        list1.sort(Integer::compareTo);
        System.out.println(list1);

        list1.sort(Integer::compare);
        System.out.println(list1);

        list1.sort((a,b) -> {
            return a.compareTo(b);
        });
        System.out.println(list1);

        //降序
        list1.sort((a,b) -> {
            return b.compareTo(a);
        });
        System.out.println(list1);


        list1.sort(Comparator.reverseOrder());
        System.out.println(list1);
    }

}
